package com.x.redcube.controller;

import com.alibaba.fastjson.JSON;
import com.x.framework.remote.HttpClient;
import com.x.redcube.model.LianJiaRequest;
import com.x.redcube.model.LianJiaResponse;
import com.x.redcube.util.Constant;
import com.x.redcube.util.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class LianJiaController {

    @Autowired
    private HttpClient httpClient;

    @RequestMapping(
            value = {"/lianjia/test"},
            method = {RequestMethod.POST, RequestMethod.GET}
    )
    public LianJiaResponse test() {
        LianJiaResponse lianJiaResponse = new LianJiaResponse(Constant.LIANJIA_GTE_001, "xxxxxxx");
        return lianJiaResponse;
    }

    @RequestMapping(
            value = {"/lianjia/assessmentData"},
            method = {RequestMethod.POST, RequestMethod.GET}
    )
    public LianJiaResponse assessmentData(LianJiaRequest lianJiaRequest) {
        LianJiaResponse lianJiaResponse = this.verifyDigest(lianJiaRequest);
        if (lianJiaResponse.getResultCode().equals(Constant.LIANJIA_GTE_001)) {
            String json = httpClient.doPostJson(Constant.REDCUBE_URL + "/lianjia/assessmentData", JSON.toJSONString(lianJiaRequest));
            lianJiaResponse = JSON.parseObject(json, LianJiaResponse.class);
        }
        return lianJiaResponse;
    }

    @RequestMapping(
            value = {"/lianjia/assessmentVideoFile"},
            method = {RequestMethod.POST, RequestMethod.GET}
    )
    public LianJiaResponse assessmentVideoFile(LianJiaRequest lianJiaRequest) {
        LianJiaResponse lianJiaResponse = this.verifyDigest(lianJiaRequest);
        if (lianJiaResponse.getResultCode().equals(Constant.LIANJIA_GTE_001)) {
            String json = httpClient.doPostJson(Constant.REDCUBE_URL + "/lianjia/assessmentVideoFile", JSON.toJSONString(lianJiaRequest));
            lianJiaResponse = JSON.parseObject(json, LianJiaResponse.class);
        }
        return lianJiaResponse;
    }

    @RequestMapping(
            value = {"/lianjia/assessmentHandleResult"},
            method = {RequestMethod.POST, RequestMethod.GET}
    )
    public LianJiaResponse assessmentHandleResult(LianJiaRequest lianJiaRequest) {
        LianJiaResponse lianJiaResponse = this.verifyDigest(lianJiaRequest);
        if (lianJiaResponse.getResultCode().equals(Constant.LIANJIA_GTE_001)) {
            String json = httpClient.doPostJson(Constant.REDCUBE_URL + "/lianjia/assessmentHandleResult", JSON.toJSONString(lianJiaRequest));
            lianJiaResponse = JSON.parseObject(json, LianJiaResponse.class);
        }
        return lianJiaResponse;
    }

    @RequestMapping(
            value = {"/lianjia/cancelAssessment"},
            method = {RequestMethod.POST, RequestMethod.GET}
    )
    public LianJiaResponse cancelAssessment(LianJiaRequest lianJiaRequest) {
        LianJiaResponse lianJiaResponse = this.verifyDigest(lianJiaRequest);
        if (lianJiaResponse.getResultCode().equals(Constant.LIANJIA_GTE_001)) {
            String json = httpClient.doPostJson(Constant.REDCUBE_URL + "/lianjia/cancelAssessment", JSON.toJSONString(lianJiaRequest));
            lianJiaResponse = JSON.parseObject(json, LianJiaResponse.class);
        }
        return lianJiaResponse;
    }

    @RequestMapping(
            value = {"/lianjia/accept"},
            method = {RequestMethod.POST, RequestMethod.GET}
    )
    public LianJiaResponse accept(LianJiaRequest lianJiaRequest) {
        LianJiaResponse lianJiaResponse = this.verifyDigest(lianJiaRequest);
        if (lianJiaResponse.getResultCode().equals(Constant.LIANJIA_GTE_001)) {
            String json = httpClient.doPostJson(Constant.LIANJIA_URL + "/assessment/cancelAssessment", JSON.toJSONString(lianJiaRequest));
            lianJiaResponse = JSON.parseObject(json, LianJiaResponse.class);
        }
        return lianJiaResponse;
    }

    @RequestMapping(
            value = {"/lianjia/yuYueKanFang"},
            method = {RequestMethod.POST, RequestMethod.GET}
    )
    public LianJiaResponse yuYueKanFang(LianJiaRequest lianJiaRequest) {
        LianJiaResponse lianJiaResponse = this.verifyDigest(lianJiaRequest);
        if (lianJiaResponse.getResultCode().equals(Constant.LIANJIA_GTE_001)) {
            String json = httpClient.doPostJson(Constant.LIANJIA_URL + "/assessment/yuYueKanFang", JSON.toJSONString(lianJiaRequest));
            lianJiaResponse = JSON.parseObject(json, LianJiaResponse.class);
        }
        return lianJiaResponse;
    }

    @RequestMapping(
            value = {"/lianjia/kanFangResult"},
            method = {RequestMethod.POST, RequestMethod.GET}
    )
    public LianJiaResponse kanFangResult(LianJiaRequest lianJiaRequest) {
        LianJiaResponse lianJiaResponse = this.verifyDigest(lianJiaRequest);
        if (lianJiaResponse.getResultCode().equals(Constant.LIANJIA_GTE_001)) {
            String json = httpClient.doPostJson(Constant.LIANJIA_URL + "/assessment/kanFangResult", JSON.toJSONString(lianJiaRequest));
            lianJiaResponse = JSON.parseObject(json, LianJiaResponse.class);
        }
        return lianJiaResponse;
    }

    @RequestMapping(
            value = {"/lianjia/makeReport"},
            method = {RequestMethod.POST, RequestMethod.GET}
    )
    public LianJiaResponse makeReport(LianJiaRequest lianJiaRequest) {
        LianJiaResponse lianJiaResponse = this.verifyDigest(lianJiaRequest);
        if (lianJiaResponse.getResultCode().equals(Constant.LIANJIA_GTE_001)) {
            String json = httpClient.doPostJson(Constant.LIANJIA_URL + "/assessment/makeReport", JSON.toJSONString(lianJiaRequest));
            lianJiaResponse = JSON.parseObject(json, LianJiaResponse.class);
        }
        return lianJiaResponse;
    }

    @RequestMapping(
            value = {"/lianjia/sendPaperReport"},
            method = {RequestMethod.POST, RequestMethod.GET}
    )
    public LianJiaResponse sendPaperReport(LianJiaRequest lianJiaRequest) {
        LianJiaResponse lianJiaResponse = this.verifyDigest(lianJiaRequest);
        if (lianJiaResponse.getResultCode().equals(Constant.LIANJIA_GTE_001)) {
            String json = httpClient.doPostJson(Constant.LIANJIA_URL + "/assessment/sendPaperReport", JSON.toJSONString(lianJiaRequest));
            lianJiaResponse = JSON.parseObject(json, LianJiaResponse.class);
        }
        return lianJiaResponse;
    }

    @RequestMapping(
            value = {"/lianjia/requestForEvidences"},
            method = {RequestMethod.POST, RequestMethod.GET}
    )
    public LianJiaResponse requestForEvidences(LianJiaRequest lianJiaRequest) {
        LianJiaResponse lianJiaResponse = this.verifyDigest(lianJiaRequest);
        if (lianJiaResponse.getResultCode().equals(Constant.LIANJIA_GTE_001)) {
            String json = httpClient.doPostJson(Constant.LIANJIA_URL + "/assessment/requestForEvidences", JSON.toJSONString(lianJiaRequest));
            lianJiaResponse = JSON.parseObject(json, LianJiaResponse.class);
        }
        return lianJiaResponse;
    }

    @RequestMapping(
            value = {"/lianjia/electronReport"},
            method = {RequestMethod.POST, RequestMethod.GET}
    )
    public LianJiaResponse electronReport(LianJiaRequest lianJiaRequest) {
        LianJiaResponse lianJiaResponse = this.verifyDigest(lianJiaRequest);
        if (lianJiaResponse.getResultCode().equals(Constant.LIANJIA_GTE_001)) {
            String json = httpClient.doPostJson(Constant.LIANJIA_URL + "/assessment/electronReport", JSON.toJSONString(lianJiaRequest));
            lianJiaResponse = JSON.parseObject(json, LianJiaResponse.class);
        }
        return lianJiaResponse;
    }

    private LianJiaResponse verifyDigest(LianJiaRequest lianJiaRequest){
        LianJiaResponse lianJiaResponse = new LianJiaResponse(Constant.LIANJIA_GTE_001, Constant.LIANJIA_SUCCESS);
        String digest = lianJiaRequest.getDigest();
        String content = JSON.toJSONString(lianJiaRequest.getContent());
        if (!DigestUtils.verify(digest, Constant.LIANJIA_PASSWORD, content)) {
            lianJiaResponse.setResultCode(Constant.LIANJIA_GTE_002);
        }
        return lianJiaResponse;
    }

}
